from django.db import models
from accounts.models import User, Technician
from services.models import Service

from django.db import models
from accounts.models import User, Technician
from services.models import Service


class Order(models.Model):
    STATUS_CHOICES = (
        ('created', '已创建'),
        ('paid', '已支付'),
        ('accepted', '已接单'),
        ('arrived', '已到达'),
        ('in_service', '服务中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
        ('refunded', '已退款'),
    )
    
    customer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders')
    technician = models.ForeignKey(Technician, on_delete=models.CASCADE, related_name='orders')
    service = models.ForeignKey(Service, on_delete=models.CASCADE)
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='created', verbose_name='订单状态')
    
    # 订单信息
    appointment_time = models.DateTimeField(verbose_name='预约时间')
    address = models.CharField(max_length=200, verbose_name='服务地址')
    contact_person = models.CharField(max_length=50, verbose_name='联系人')
    contact_phone = models.CharField(max_length=20, verbose_name='联系电话')
    remarks = models.TextField(blank=True, verbose_name='备注')
    
    # 价格信息
    original_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='原价')
    discount = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='折扣')
    final_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='最终价格')
    
    # 时间信息
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    paid_at = models.DateTimeField(null=True, blank=True, verbose_name='支付时间')
    completed_at = models.DateTimeField(null=True, blank=True, verbose_name='完成时间')
    
    class Meta:
        db_table = 'order_info'
        verbose_name = '订单信息'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"订单#{self.id} - {self.customer.username}"


class Review(models.Model):
    order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='review')
    customer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reviews')
    technician = models.ForeignKey(Technician, on_delete=models.CASCADE, related_name='reviews')
    
    rating = models.IntegerField(choices=[(i, i) for i in range(1, 6)], verbose_name='评分')  # 1-5星
    comment = models.TextField(verbose_name='评价内容')
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'review'
        verbose_name = '订单评价'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"评价#{self.id} - 订单#{self.order.id}"